<template>
  <div>
    <el-button type="primary" @click="addNum">新增按钮</el-button>
    <el-table :data="list" height="300px" border style="width: 1500px">
      <el-table-column
        v-for="(item, index) in tableTitle"
        align="center"
        :key="index"
        :prop="item.prop"
        :label="item.label"
      >
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template scope="{row}">
          <el-link
            type="primary"
            style="margin: 0 5px"
            @click="handleEdit(row, 'edit')"
            >编辑</el-link
          >
          <el-link
            type="primary"
            style="margin: 0 5px"
            @click="handleEdit(row, 'del')"
            >删除</el-link
          >
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="30%">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="编号" prop="id">
          <el-input disabled v-model="ruleForm.id"></el-input>
        </el-form-item>
        <el-form-item label="店铺名称" prop="title">
          <el-input v-model="ruleForm.title"></el-input>
        </el-form-item>
        <el-form-item label="店铺介绍" prop="price">
          <el-input v-model="ruleForm.price"></el-input>
        </el-form-item>
        <el-form-item label="店铺地址" prop="num">
          <el-input v-model="ruleForm.num"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClear('ruleForm')">取 消</el-button>
        <el-button type="primary" @click="handleEnsure('ruleForm')"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapMutations, mapState} from "vuex";
import "../../mock/index";
export default {
  data() {
    return {
      dialogTitle: "编辑",
      ruleForm: {
        title: "",
        price: "",
        id: "",
        num: "",
      },
      rules: {
        title: [{ required: true, message: "请输入店铺名称", trigger: "blur" }],
        price: [{ required: true, message: "请输入店铺地址", trigger: "blur" }],
        id: [{ required: true, message: "请输入商品编号", trigger: "blur" }],
        num: [{ required: true, message: "请输入店铺介绍", trigger: "blur" }],
      },
      dialogVisible: false,
      tableTitle: [
        {
          prop: "id",
          label: "编号",
        },
        {
          prop: "title",
          label: "店铺名称",
        },
        {
          prop: "price",
          label: "店铺介绍",
        },
        {
          prop: "num",
          label: "店铺地址",
        },
      ],
    };
  },
  created() {
    this.$store.dispatch("getList");
  },
  computed: {
    ...mapState(["list"]),
  },
  methods: {
    ...mapMutations(['addNums','edit','del']),
    addNum() {
        this.ruleForm.id = new Date() * 1;
      this.dialogTitle = "新增商品";
      this.dialogVisible = true;
    
    },
    handleClear() {
      this.dialogVisible = false;  
      this.ruleForm = {
        title: "",
        price: "",
        id: "",
        num: "",
      };
    },
    handleEnsure(name) {
      this.$refs[name].validate((valid) => {
        if (valid) {
          if (this.dialogTitle == "新增商品") {
            this.addNums(this.ruleForm)
            this.dialogVisible=false
          } else {
            this.edit(this.ruleForm);
           this.handleClear("ruleForm");
            this.dialogVisible=false
            this.$message({
                  type: "success",
                  message: "编辑完成",
                });
          }
        } else {
          return false;
        }
      });
    },
    handleEdit(row, type) {
      if (type == "del") {
        this.$confirm("此操作删除该条记录, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            this.del(row)
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
          });
      } else {
        //推荐单个赋值
        this.ruleForm.title = row.title;
        this.ruleForm.price = row.price;
        this.ruleForm.id = row.id;
        this.ruleForm.num = row.num;
        this.dialogVisible = true;
      }
    },
  },
};
</script>

<style>

</style>